package org.tgcms.core.dal.appengine.dao;

import org.tgcms.core.dal.appengine.domain.AppengineRedirect;
import org.tgcms.core.dal.appengine.domain.AppengineWebsite;
import org.tgcms.core.dal.dao.DaoException;
import org.tgcms.core.dal.dao.RedirectDao;
import org.tgcms.core.dal.dto.Redirect;
import org.tgcms.core.dal.dto.Website;

import com.google.appengine.api.datastore.Key;

public class AppengineRedirectDao extends
		AppengineEntityDao<AppengineRedirect, Redirect, Website> implements
		RedirectDao {

	public AppengineRedirectDao() {
		super(AppengineRedirect.class, Redirect.class);
	}

	@Override
	protected AppengineRedirect asAppengineEntity(Redirect redirect)
			throws DaoException {
		if (redirect instanceof AppengineRedirect) {
			return (AppengineRedirect) redirect;
		}
		return null;// new AppengineRedirect(redirect);
	}

	@Override
	public Redirect create(Website website) throws DaoException {
		AppengineWebsite appengineWebsite = checkWebsite(website);
		Key key = datastore
				.allocateIds(appengineWebsite.getKey(),
						AppengineRedirect.class.getSimpleName(), 1).iterator()
				.next();
		return new AppengineRedirect(key);
	}

}
